<?php
namespace App\Service;

use App\Exception\Handler\BusinessException;
use Hyperf\DbConnection\Db;


abstract class BaseService
{

    /**
     * 抛出异常
     * @param int $code
     * @param string $message
     */
    public function throwExp($code = 0, $message = '')
    {
        if (empty($code)) $code = 500;

        Throw new BusinessException($code, $message);
    }

    /**
     * 对象转为数组
     * @param string $data
     * @return mixed
     */
    public static function toArray($data = '')
    {
        return json_decode(json_encode($data), true);
    }

    /**
     * @todo 根据用户id 获取用户信息
     * @param $uid
     * @return array
     */
    public static function getUserInfoByUid($uid) {
        $users = Db::table('users')->where("id",$uid)->first();
        return self::toArray($users);
    }

    /**
     * @todo 返回用户注册天数
     * @return int
     */
    protected static  function userRegDays($userInfo):int {
        $user_info=$userInfo;
        $regDayTime = strtotime(date('Y-m-d',strtotime($user_info['reg_time'])));
        $todayTime = strtotime(date('Y-m-d'));
        $regDays = intval(($todayTime - $regDayTime)/(24*60*60));
        $regDays = $regDays + 1;
        return $regDays;
    }

    /**
     * @todo 返回广告表
     * @param $tablePrefixName
     * @return string
     */
    protected static function getDateTable($tablePrefixName):string {
        $date = date('Ymd');
//        $redis = RedisService::getInstance();
//        $tableName = $tablePrefixName.'_'.$date;
//        $tableNameKey = 'jingling_'.$tableName;
//        if(!$redis->get($tableNameKey)){
//            $createTableSql = "create table if not exists ".$tableName." like ".$tablePrefixName;
//            \think\Db::execute($createTableSql);
//            $redis->set($tableNameKey,1,24*3600);
//        }
//        return $tableName;
        return "";
    }


}
